[Django] Change state of obiects

Posted by gameboy on Stack Overflow See other posts from Stack Overflow or by gameboy
Published on 2010-03-24T13:34:31Z Indexed on 2010/03/24 14:13 UTC
Read the original article Hit count: 295

Filed under:

hi I have following problem.

I have model:

 class Towar(models.Model):
  nrSeryjny=models.CharField(max_length=100)
  opis=models.CharField(max_length=255)
  naStanie=models.NullBooleanField(null=True)



  def __unicode__(self):
    return "%s" % self.opis

  def lowerName(self):
    return self.__class__.__name__.lower()

  def checkState(self):
    return self.naStanie

  def changeState(self,state):
    self.naStanie=state


  class Meta:
    ordering=['nrSeryjny']
    app_label = 'baza'
    permissions=(("view_towar","mozna miec podglad dla towar"),)

and model :

class Wypozyczenie(models.Model):
  dataPobrania=models.DateField()
  pracownik=models.ForeignKey(User,null=True)
  kontrahent=models.ForeignKey(Kontrahenci,null=True)
  towar=models.ForeignKey(Towar,null=True)
  objects=WypozyczenieManager()
  default_objects=models.Manager()
  ZwrotyObjects=WypozyczenieZwrotyManager()

  def lowerName(self):
    return self.__class__.__name__.lower()

  def __unicode__(self):
    if self.towar == None:
      return "Dla:%s -- Kto:%s -- Kiedy:%s -- Co:%s" % (self.kontrahent,self.pracownik,self.dataPobrania,"Brak")
    else:
      return "Dla:%s -- Kto:%s -- Kiedy:%s -- Co:%s" % (self.kontrahent,self.pracownik,self.dataPobrania,self.towar)

  class Meta:
    ordering=['dataPobrania']
    app_label = 'baza'
    permissions=(("view_wypozyczenie","mozna miec podglad dla wypozyczenie"),)

and view to adding models:

def modelAdd(request,model,modelForm):
  mod=model()
  if request.user.has_perm('baza.add_%s' % mod.lowerName()):
    if request.method=='POST':
      form=modelForm(request.POST)
      if form.is_valid():
        form.save()
        return HttpResponseRedirect('/'+ mod.lowerName() + '/')
    else:
      form=modelForm()
    v=RequestContext(request,{'form':form})
    return render_to_response('add_form.html',v)

and i whant do that, when i add Wypozyczenie and save it then the Towar that is stored by Wypozyczenie change his na stanie from True to False

Greets

© Stack Overflow or respective owner

Related posts about django